def file = new File('../GroovyDemo.iml')

//每一行没有换行符
//file.eachLine {
//    print it
//}

//全部带换行符
//String result = file.getText()
//println result

//每一行没有换行符
//List<String> result = file.readLines()
//result.each {
//    print it
//}

//读取部分内容
//def reader = file.withReader {
//    def buffer = new char[100]
//    it.read(buffer)
//    buffer
//}
//
//println reader

//println copy('../GroovyDemo.iml', '../GroovyDemo1.iml')

def copy(String srcPath, String targetPath) {
    try {
        new File(srcPath).withReader { reader ->
            def content = reader.getText()
            new File(targetPath).withWriter { writer ->
                writer.write(content)
            }
        }
        true
    } catch (Exception ignored) {
        false
    }
}

//println saveObj(new Person(name: 'cherry',age:18),'../Person.bin')

//Person person = (Person) getObj('../Person.bin')
//
//println "person name is ${person?.name},age is ${person?.age}"
//
//def getObj(String targetPath) {
//    Object obj = null
//    def file = new File(targetPath)
//    if (file == null || !file.exists()) {
//        return null
//    }
//
//    try {
//        file.withObjectInputStream { reader ->
//            obj = reader.readObject()
//        }
//    } catch (Exception e) {
//        e.printStackTrace()
//    }
//    return obj
//}
//
//
//def saveObj(Object obj, String targetPath) {
//
//    def file = new File(targetPath)
//
//    try {
//        file.withObjectOutputStream { writer ->
//            writer.writeObject(obj)
//        }
//        true
//    } catch (Exception ignored) {
//        false
//    }
//}
//
//class Person implements Serializable{
//    String name
//    int age
//}





